﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Ticket
{
    public partial class frmTicket : frmBase
    {
        #region Contructor
        public frmTicket()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.Invoice boInvoice = new Business.Invoice();
        private Business.Voucher boVoucher = new Business.Voucher();
        private Business.VoucherHistory boVoucherHistory = new Business.VoucherHistory();
        private Business.Employee boEmployee = new Business.Employee();
        public string VOUCHER_NO = "";
        #endregion

        #region Event
        private void frmTicket_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Chi tiết vé";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_01");

            LoadStatusOnce();
            LoadStatus(false, false, false, true, false);

            GetVoucher();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                ArrayList arrResult = new ArrayList();
                if (objMethod.IsDouble(txtSearch.Text))
                {
                    DataObjects.Voucher oVou = boVoucher.GetObjectByPK(txtSearch.Text);
                    if (oVou != null)
                    {
                        arrResult.Add(oVou);
                    }
                }
                else
                {
                    ArrayList arr1 = boVoucher.GetListQuickSearch(txtSearch.Text);
                    if (arr1 != null)
                    {
                        if (arr1.Count == 1)
                        {
                            arrResult.Add(arr1[0]);
                        }
                        else if (arr1.Count > 1)
                        {
                            frmQuickSearchVoucher frmQS = new frmQuickSearchVoucher(arr1);
                            frmQS.ShowDialog();
                            foreach (DataObjects.Voucher oVou in frmQS.arrResult)
                            {
                                arrResult.Add(oVou);
                            }
                        }
                    }
                }

                ListViewItem item;
                int firstItem = 0;

                foreach (DataObjects.Voucher oVou in arrResult)
                {
                    if (!IsInListViewVoucher(oVou.VoucherNo))
                    {
                        item = new ListViewItem();
                        item.Text = oVou.VoucherNo;
                        lvwVoucher.Items.Add(item);

                        if (firstItem == 0)
                        {
                            item.Selected = true;
                        }
                        firstItem++;
                    }
                }
            }
        }

        private void lvwVoucher_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwVoucher.SelectedItems.Count > 0)
            {
                VOUCHER_NO = lvwVoucher.SelectedItems[0].Text;
                LoadStatus(true, true, true, true, true);
                GetListHistory(VOUCHER_NO);
                GetListInvoice(VOUCHER_NO);
                FillDetail(VOUCHER_NO);
            }
            else
            {
                VOUCHER_NO = "";
                LoadStatus(true, false, false, true, false);
            }
        }

        private void lvwVoucher_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                if ((lvwVoucher.SelectedItems.Count > 0))
                {
                    foreach (ListViewItem item in lvwVoucher.SelectedItems)
                    {
                        lvwVoucher.Items.Remove(item);
                    }
                }
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            Ticket.frmVoucher frmNewVoucher = new ManageTicket.Ticket.frmVoucher("");
            frmNewVoucher.ShowDialog();
        }

        public override void Edit()
        {
            Ticket.frmVoucher frmNewVoucher = new ManageTicket.Ticket.frmVoucher(VOUCHER_NO);
            frmNewVoucher.ShowDialog();

            if (!CheckIsExist(VOUCHER_NO))
            {
                foreach (ListViewItem item in lvwVoucher.Items)
                {
                    if (item.Text == VOUCHER_NO)
                        lvwVoucher.Items.Remove(item);
                }
                VOUCHER_NO = "";
                LoadStatus(true, false, false, true, false);
            }
        }

        public override void Find()
        {
            frmFindTicket frmFind = new frmFindTicket();
            frmFind.ShowDialog();

            //ArrayList arrResult = frmFind.arrResult;
            ArrayList arrResult = ManageTicket.Library.Common.CommonList.ListVoucher;
            ListViewItem item;
            int firstItem = 0;

            foreach (DataObjects.Voucher oVou in arrResult)
            {
                if (!IsInListViewVoucher(oVou.VoucherNo))
                {
                    item = new ListViewItem();
                    item.Text = oVou.VoucherNo;
                    lvwVoucher.Items.Add(item);

                    if (firstItem == 0)
                    {
                        item.Selected = true;
                    }
                    firstItem++;
                }
            }
        }

        public override void Print()
        {
            frmPrintTicket frm = new frmPrintTicket(VOUCHER_NO);
            frm.ShowDialog();
        }

        public override void Function()
        {
            frmUpdateStatusVoucher objUSV = new frmUpdateStatusVoucher(VOUCHER_NO);
            objUSV.ShowDialog();
        }
        #endregion

        #region Method
        private bool IsInListViewVoucher(string voucher_no)
        {
            foreach (ListViewItem item in lvwVoucher.Items)
            {
                if (item.Text == voucher_no)
                {
                    return true;
                }
            }
            return false;
        }

        private void LoadStatusOnce()
        {
            exeCtrl.btnDelete.Enabled = false;
            exeCtrl.btnCancel.Enabled = false;
            exeCtrl.btnSave.Enabled = false;
            //exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnFind.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;

            exeCtrl.btnDelete.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = Color.FromArgb(171, 171, 171);
            //exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnDelete.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = Color.FromArgb(241, 244, 251);
            //exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blFunction, bool blFind, bool blPrint)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnFunction.Enabled = blFunction & bFunction;
            exeCtrl.btnPrint.Enabled = blPrint & bPrint;
            exeCtrl.btnFind.Enabled = blFind & bFind;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnFunction.BackColor = exeCtrl.btnFunction.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = exeCtrl.btnPrint.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnFunction.ForeColor = exeCtrl.btnFunction.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = exeCtrl.btnPrint.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void GetVoucher()
        {
            if (ManageTicket.Library.Common.CommonList.ListVoucher != null)
            {
                foreach (DataObjects.Voucher oVou in ManageTicket.Library.Common.CommonList.ListVoucher)
                {
                    ListViewItem item = new ListViewItem();
                    if (!IsInListViewVoucher(oVou.VoucherNo))
                    {
                        item.Text = oVou.VoucherNo;
                        lvwVoucher.Items.Add(item);
                    }
                }
                if (lvwVoucher.Items.Count > 0)
                {
                    lvwVoucher.Items[0].Selected = true;
                }
            }
            
        }

        private void GetListHistory(string voucher_no)
        {
            try
            {
                lvwHistory.Items.Clear();
                Business.CodeType boCodeType = new Business.CodeType();
                ArrayList arr = boVoucherHistory.GetListByVoucherNo(voucher_no);
                if (arr.Count > 0)
                {
                    ListViewItem item;
                    DataObjects.CodeType oCodeType;
                    double profix_usd = 0;
                    double profix_vnd = 0;
                    foreach (DataObjects.VoucherHistory oVouHis in arr)
                    {
                        item = new ListViewItem();
                        item.Text = oVouHis.DateCreate.Value.ToString("dd/MM/yyyy hh:mm tt");
                        oCodeType = boCodeType.GetObjectByPK(oVouHis.Status);
                        if (oCodeType != null)
                            item.SubItems.Add(oCodeType.CodeTypeName);
                        else
                            item.SubItems.Add("");
                        oCodeType = null;

                        item.SubItems.Add(objMethod.FormatCurency(oVouHis.OutgoingUsd.ToString()));
                        item.SubItems.Add(objMethod.FormatCurency(oVouHis.IncomingUsd.ToString()));
                        try
                        {
                            item.SubItems.Add(objMethod.FormatCurency((oVouHis.IncomingUsd - oVouHis.OutgoingUsd).ToString()));
                            profix_usd += oVouHis.IncomingUsd - oVouHis.OutgoingUsd;
                        }
                        catch { item.SubItems.Add("0"); }

                        item.SubItems.Add(objMethod.FormatCurency(oVouHis.OutgoingVnd.ToString()));
                        item.SubItems.Add(objMethod.FormatCurency(oVouHis.IncomingVnd.ToString()));
                        try
                        {
                            item.SubItems.Add(objMethod.FormatCurency((oVouHis.IncomingVnd - oVouHis.OutgoingVnd).ToString()));
                            profix_vnd += oVouHis.IncomingVnd - oVouHis.OutgoingVnd;
                        }
                        catch { item.SubItems.Add("0"); }

                        item.SubItems.Add(oVouHis.Remark);
                        lvwHistory.Items.Add(item);
                    }

                    item = new ListViewItem();
                    item.Text = "";
                    item.SubItems.Add("");
                    item.SubItems.Add("");
                    item.SubItems.Add("Tổng:");
                    item.SubItems.Add(objMethod.FormatCurency(profix_usd.ToString()));
                    item.SubItems.Add("");
                    item.SubItems.Add("Tổng");
                    item.SubItems.Add(objMethod.FormatCurency(profix_vnd.ToString()));
                    item.SubItems.Add("");
                    item.BackColor = Color.Aqua;
                    lvwHistory.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GetListInvoice(string voucher_no)
        {
            try
            {
                lvwInvoice.Items.Clear();
                Business.CodeType boCodeType = new Business.CodeType();
                ArrayList arr = boInvoice.GetListByVoucherNo(voucher_no);
                if (arr.Count > 0)
                {
                    ListViewItem item;
                    DataObjects.CodeType oCodeType;
                    foreach (DataObjects.Invoice oInv in arr)
                    {
                        item = new ListViewItem();
                        item.Text = oInv.InvoiceNo;
                        item.SubItems.Add(oInv.DateOfInvoice.Value.ToString("dd/MM/yyyy hh:mm tt"));
                        oCodeType = boCodeType.GetObjectByPK(oInv.KindOfVoucher);
                        if (oCodeType != null)
                            item.SubItems.Add(oCodeType.CodeTypeName);
                        else
                            item.SubItems.Add("");
                        oCodeType = null;
                        item.SubItems.Add(oInv.KindOfInvoice);
                        item.SubItems.Add(objMethod.FormatCurency(oInv.Money.ToString()));
                        oCodeType = boCodeType.GetObjectByPK(oInv.Currency);
                        if (oCodeType != null)
                            item.SubItems.Add(oCodeType.CodeTypeName);
                        else
                            item.SubItems.Add("");
                        oCodeType = null;
                        oCodeType = boCodeType.GetObjectByPK(oInv.PaymentForm);
                        if (oCodeType != null)
                            item.SubItems.Add(oCodeType.CodeTypeName);
                        else
                            item.SubItems.Add("");
                        oCodeType = null;
                        item.SubItems.Add(oInv.Remark);
                        lvwInvoice.Items.Add(item);
                    }

                    if (lvwInvoice.Items.Count > 0)
                    {
                        lvwInvoice.Items[0].Selected = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FillDetail(string voucher_no)
        {
            try
            {
                Business.Customer boCustomer = new Business.Customer();
                Business.Provider boProvider = new Business.Provider();
                Business.CodeType boCodeType = new Business.CodeType();
                DataObjects.CodeType oCodeType;
                DataObjects.Voucher oVou = boVoucher.GetObjectByPK(voucher_no);
                if (oVou != null)
                {
                    txtVoucherNo.Text = oVou.VoucherNo;
                    dtpDateOfVoucher.Value = oVou.DateOfVoucher.Value;
                    txtEmployeeId.Text = oVou.EmployeeId;
                    DataObjects.Employee oEmp = boEmployee.GetObjectByPK(oVou.EmployeeId);
                    if (oEmp != null)
                        txtFullName.Text = oEmp.LastName + " " + oEmp.FirstName;
                    txtCustomerNameDirect.Text = oVou.CustomerNameDirect;
                    txtTicketNo.Text = oVou.TicketNo;
                    oCodeType = boCodeType.GetObjectByPK(oVou.TransportId);
                    if (oCodeType != null)
                        txtTransport.Text = oCodeType.CodeTypeName;
                    oCodeType = null;
                    oCodeType = boCodeType.GetObjectByPK(oVou.ServiceId);
                    if (oCodeType != null)
                        txtService.Text = oCodeType.CodeTypeName;
                    oCodeType = null;
                    txtTour.Text = oVou.Tour;
                    txtRemark.Text = oVou.Remark;

                    //provider
                    txtProviderId.Text = oVou.ProviderId;
                    txtProviderPersonRepresent.Text = oVou.ProviderPersonRepresent;
                    DataObjects.Provider oPro = boProvider.GetObjectByPK(oVou.ProviderId);
                    if (oPro != null)
                        txtProviderName.Text = oPro.ProviderName;
                    txtBuyTotal.Text = objMethod.FormatCurency(oVou.BuyTotal.ToString());
                    txtBuyVATPercent.Text = oVou.BuyVatPercent.ToString();
                    txtBuyVAT.Text = objMethod.FormatCurency(oVou.BuyVat.ToString());
                    txtBuySum.Text = objMethod.FormatCurency(oVou.BuySum.ToString());
                    oCodeType = boCodeType.GetObjectByPK(oVou.BuyCurrency);
                    if (oCodeType != null)
                        txtBuyCurrency.Text = oCodeType.CodeTypeName;
                    oCodeType = null;
                    txtBuyExchange.Text = objMethod.FormatCurency(oVou.BuyExchange.ToString());
                    txtBuySumVND.Text = objMethod.FormatCurency(oVou.BuySumVnd.ToString());

                    //customer  
                    txtCustomerId.Text = oVou.CustomerId;
                    txtCustomerPersonRepresent.Text = oVou.CustomerPersonRepresent;
                    DataObjects.Customer oCus = boCustomer.GetObjectByPK(oVou.CustomerId);
                    if (oCus != null)
                        txtCustomerName.Text = oCus.CustomerName;
                    txtSaleTotal.Text = objMethod.FormatCurency(oVou.SaleTotal.ToString());
                    txtSaleVATPercent.Text = oVou.SaleVatPercent.ToString();
                    txtSaleVAT.Text = objMethod.FormatCurency(oVou.SaleVat.ToString());
                    txtSaleSum.Text = objMethod.FormatCurency(oVou.SaleSum.ToString());
                    oCodeType = boCodeType.GetObjectByPK(oVou.SaleCurrency);
                    if (oCodeType != null)
                        txtSaleCurrency.Text = oCodeType.CodeTypeName;
                    oCodeType = null;
                    txtSaleExchange.Text = objMethod.FormatCurency(oVou.SaleExchange.ToString());
                    txtSaleSumVND.Text = objMethod.FormatCurency(oVou.SaleSumVnd.ToString());
                    txtSaleCommission.Text = objMethod.FormatCurency(oVou.SaleCommission.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateVoucherStatus(string voucher_no)
        {
            //DataObjects.Voucher oVou = boVoucher.GetObjectByPK(voucher_no);
            //if (oVou != null)
            //{
            //    if (cboStatus.Text == "Hiện tại")
            //        oVou.Status = "CURRENT";
            //    else if (cboStatus.Text == "Kết thúc")
            //        oVou.Status = "FINISH";
            //    else
            //        oVou.Status = "CURRENT";

            //    boVoucher.Update(oVou);
            //}
        }

        private void GetVoucherStatus(string voucher_no)
        {
            //DataObjects.Voucher oVou = boVoucher.GetObjectByPK(voucher_no);
            //if (oVou != null)
            //{
            //    if (oVou.Status.ToUpper() == "CURRENT")
            //        cboStatus.Text = "Hiện tại";
            //    else if (oVou.Status.ToUpper() == "FINISH")
            //        cboStatus.Text = "Kết thúc";
            //}
        }

        private bool CheckIsExist(string voucher_no)
        {
            DataObjects.Voucher oVou = boVoucher.GetObjectByPK(voucher_no);
            if (oVou != null)
            {
                return true;
            }
            return false;
        }
        #endregion

    }
}
